package cn.cvs.service.sysUser;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.supplier.SupplierService;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static org.junit.Assert.*;

public class SysUserTest {
    private Logger logger = Logger.getLogger(SysUserTest.class);

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testGetUserList() {
//        String [] configs={"applicationContext.xml","applicationContext-dao.xml","applicationContext-service.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService userService = (SysUserService) ctx.getBean("sysUserService");
        List<SysUser> userList = new ArrayList<SysUser>();
        SysUser userCondition = new SysUser();
        userCondition.setRealName("赵");
        userCondition.setRoleId(2);
        userList = userService.getList(userCondition);

        for (SysUser userResult : userList) {
            logger.debug("testGetUserList account:"
                    + userResult.getAccount() + "and realName:"
                    + userResult.getRealName() + "and roleId:"
                    + userResult.getRoleId() + "and roleName:"
                    + userResult.getRoleIdName() + "and address:"
                    + userResult.getAddress());
        }
    }

    @Test
    public void testAddUser() throws ParseException{
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");
        SysUserService userService=(SysUserService) applicationContext.getBean("sysUserService");
        SysUser user=new SysUser();
        user.setAccount("test002");
        user.setRealName("测试用户002");
        user.setPassword("1234567");
        Date birthday = new SimpleDateFormat("yyyy-MM-dd").parse("2003-10-22");
        user.setBirthday(birthday);
        user.setAddress("测试地址abc");
        user.setSex(1);
        user.setPhone("13411110000");
        user.setRoleId(4);
        user.setCreatedUserId(1);
        user.setCreatedTime(new Date());
        int result=userService.add(user);
        logger.debug("testAdd result"+result);
    }
}